




//
//  MBSeletedCVC.m
//  发布求租
//
//  Created by MB__Lee on 2018/6/7.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBSeletedCVC.h"

#import "MBSelectedCell.h"

#import <Masonry.h>

@interface MBSeletedCVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableview;

@property(nonatomic,strong)NSMutableArray *selectedMut; // 选中的数据

@property(nonatomic,strong)UIButton *finishBTN;
@property(nonatomic,strong)UIView *btnView;

@end

static NSString *MBSelectedCell_Identi = @"MBSelectedCell";

@implementation MBSeletedCVC
-(NSMutableArray *)selectedMut{
    if (!_selectedMut) {
        _selectedMut = [NSMutableArray array];
    }
    return _selectedMut;
}
-(UIButton *)finishBTN{
    if (!_finishBTN) {
        _finishBTN = [UIButton buttonWithType:UIButtonTypeCustom];
        [_finishBTN setTitle:@"完成" forState:UIControlStateNormal];
        [_finishBTN setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _finishBTN.backgroundColor = MB_mainSelectedColor;
        _finishBTN.frame = CGRectMake(0, 0, SCREEN_WIDTH, 60);
        
        [_finishBTN addTarget:self action:@selector(makeFinish:) forControlEvents:UIControlEventTouchUpInside];
        
       
        
        
    }
    return _finishBTN;
}
-(UIView *)btnView{
    if (!_btnView) {
        _btnView  = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 81)];
        
        _btnView.backgroundColor = [UIColor whiteColor];
        [_btnView addSubview:self.finishBTN];
    }
    return  _btnView;
}
-(void)makeFinish:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(MBSeletedCVC:SendSelectedMut:)]) {
        
        NSMutableArray *mut = [NSMutableArray array];
        
        [self.selectedMut sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSIndexPath *indexPaht1 = (NSIndexPath *)obj1;
            NSIndexPath *indexPaht2 = (NSIndexPath *)obj2;
            return indexPaht1.row > indexPaht2.row;
        }];
        
        for (NSIndexPath *indexPath in self.selectedMut) {
            [mut addObject:self.contentMut[indexPath.row]];
        }
        
        [self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
        
        [self.delegate MBSeletedCVC:self SendSelectedMut:mut];
    }
    
}

-(UITableView *)tableview{
    if (!_tableview) {
        
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.contentInset = UIEdgeInsetsMake(0, 0, 81, 0);
        
        _tableview.rowHeight = 50;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_tableview registerNib:[UINib nibWithNibName:MBSelectedCell_Identi bundle:nil] forCellReuseIdentifier:MBSelectedCell_Identi];
        
    }
    return  _tableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    if (self.type == CVCType_houseResidentOritation) {
        return;
    }

    [self.view addSubview:self.tableview];
    [self.view addSubview:self.btnView];
    [self makeLayoutWithFinishBTN];
}
-(void)makeLayoutWithFinishBTN{
    [self.btnView  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(@81);
    }];
    [self.finishBTN mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.btnView).offset(18);
        make.bottom.mas_equalTo(self.btnView).offset(-18);
        make.left.mas_equalTo(self.btnView).offset(30);
        make.right.mas_equalTo(self.btnView).offset(-30);
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.contentMut.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MBSelectedCell *cell =[tableView dequeueReusableCellWithIdentifier:MBSelectedCell_Identi];
    cell.name = self.contentMut[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MBSelectedCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.isAdd) {
        [self.selectedMut addObject:indexPath];
    }else{
        [self.selectedMut removeObject:indexPath];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
